package com.surpassun.emall.controller;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.surpassun.emall.model.Product;
import com.surpassun.emall.service.ProductService;
import com.surpassun.emall.util.Constants;
import com.surpassun.emall.util.ParamUtil;

@Controller
@RequestMapping("/search")
public class SearchController {
	
	@Inject
	ProductService productService;

	@RequestMapping
	public String search(HttpServletRequest request, Model model) {
		String keyword = ParamUtil.getString(request, "keyword");
		//If cityId == 0, means all of the country
		long cityId = ParamUtil.getLong(request, "city");
		
		List<Product> products = productService.searchProducts(keyword, cityId);
		model.addAttribute(Constants.ATTR_KEYWORD, keyword);
		model.addAttribute(Constants.ATTR_PRODUCTS, products);
		
		return "search";
	}
}
